//
//  CLBannerView.m
//  RainbowFM
//
//  Created by chenli on 14/12/20.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "CLBannerView.h"
#import "GoodsCatalogModel.h"
#import "BannerCell.h"

@interface CLBannerView()<UICollectionViewDataSource,UICollectionViewDelegate>{
    NSMutableArray *listArray;
    NSMutableArray *secondArray;
    NSInteger firstSelectedIndex;
    NSInteger secondSelectedIndex;
    BOOL secondShow;
}

@end


@implementation CLBannerView

-(void)awakeFromNib{
    listArray = [NSMutableArray array];
    secondArray = [NSMutableArray array];
    secondShow = NO;
    firstSelectedIndex = 0;
    secondSelectedIndex = -1;
    [self fetchFirstCatalogList];
    
    self.layer.masksToBounds = YES;
}


-(void)fetchFirstCatalogList{
    [self.collectionView registerNib:[UINib nibWithNibName:@"BannerCell" bundle:nil] forCellWithReuseIdentifier:@"BannerCell"];
    [self.collectionView2 registerNib:[UINib nibWithNibName:@"BannerCell" bundle:nil] forCellWithReuseIdentifier:@"BannerCell"];
    
    [[GoodsCatalogModel sharedModel] fetchGoodsFirstCatalogList:^(NSArray *array, NSString *msg) {
        [listArray addObjectsFromArray:array];
        GoodsFirstCatalogItem *item = [[GoodsFirstCatalogItem alloc] init];
        item.CATALOG_NAME = @"全部";
        [listArray insertObject:item atIndex:0];
        [self.collectionView reloadData];
    }];
}

-(void)fetchSecondCatalogList:(NSString *)Id{
    [secondArray removeAllObjects];
    [[GoodsCatalogModel sharedModel] fetchgoodsSecondCatalogListWithCatalogID:Id result:^(NSArray *array, NSString *msg) {
        [secondArray addObjectsFromArray:array];
        [self.collectionView2 reloadData];
    }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView.tag == 1) {
        return listArray.count;
    }
    else if(collectionView.tag == 2){
        return secondArray.count;
    }
    else
        return 0;
    
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BannerCell" forIndexPath:indexPath];
    
    if (collectionView.tag == 1) {
        GoodsFirstCatalogItem *item = listArray[indexPath.row];
        cell.titleLabel.text = item.CATALOG_NAME;
        if (indexPath.row == firstSelectedIndex) {
            [cell selected:YES];
        }else{
            [cell selected:NO];
        }
        
        if (indexPath.row == listArray.count -1) {
            cell.grayView.hidden= YES;
        }else{
            cell.grayView.hidden = NO;
        }
        
    }else if(collectionView.tag == 2){
        GoodsFirstCatalogItem *item = secondArray[indexPath.row];
        cell.titleLabel.text = item.CATALOG_NAME;
        if (indexPath.row == secondSelectedIndex) {
            [cell selected:YES];
        }else{
            [cell selected:NO];
        }
        
        if (indexPath.row == secondArray.count -1) {
            cell.grayView.hidden= YES;
        }
        else
        {
            cell.grayView.hidden = NO;
        }
    }
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView.tag == 1) {
        GoodsFirstCatalogItem *item = listArray[indexPath.row];
        
        if (firstSelectedIndex != indexPath.row) {
            secondSelectedIndex = -1;
        }
        
        firstSelectedIndex = indexPath.row;
        [self.collectionView reloadData];
        [self fetchSecondCatalogList:item.CATALOG_ID];
        
        if (indexPath.row == 0) {
            [self hiddenSecondCollectionView];
            if (_delegate && [_delegate respondsToSelector:@selector(bannerSelectedFirstID:sencondID:)]) {
                [_delegate bannerSelectedFirstID:@"" sencondID:@""];
            }
        }else if(!secondShow)
            [self showSecondCollectionView];
        
    }
    else{
        secondSelectedIndex = indexPath.row;
        [self.collectionView2 reloadData];
        [self hiddenSecondCollectionView];
        
        if (_delegate && [_delegate respondsToSelector:@selector(bannerSelectedFirstID:sencondID:)]) {
            GoodsFirstCatalogItem *item2 = secondArray[indexPath.row];
            GoodsFirstCatalogItem *item1 = listArray[firstSelectedIndex];
//            [_delegate bannerSelected:item.CATALOG_ID];
            [_delegate bannerSelectedFirstID:item1.CATALOG_ID sencondID:item2.CATALOG_ID];
        }
        
    }
    
}


-(void)showSecondCollectionView{
    secondShow = YES;
    CGRect selfFrame = self.frame;
    CGRect superFrame = self.superview.frame;
    selfFrame.size.height = CGRectGetHeight(self.collectionView2.frame) + CGRectGetHeight(self.collectionView.frame);
    superFrame.size.height = selfFrame.size.height;
    [UIView animateWithDuration:0.15 animations:^{
        self.frame = selfFrame;
        self.superview.frame = superFrame;
    } completion:^(BOOL finished) {
        if (finished) {
//            [_delegate bannerFrameChange:self.frame];
        }
    }];
}


-(void)hiddenSecondCollectionView{
    secondShow = NO;
    CGRect selfFrame = self.frame;
    CGRect superFrame = self.superview.frame;
    selfFrame.size.height = CGRectGetHeight(self.collectionView.frame);
    superFrame.size.height = selfFrame.size.height;
    [UIView animateWithDuration:0.15 animations:^{
        self.superview.frame = superFrame;
        self.frame = selfFrame;
        
    }];
}

@end
